<template>
  <el-popover
    placement="right-end"
    trigger="click"
    width="1000"
    @show="show"
    v-model="visible">
    <el-input class="pop-input" :size="size" slot="reference" clearable placeholder="点击选择同义词"></el-input>
    <split-pane v-on:resize="resize" :default-percent="30" :min-percent='20' split="vertical" style="height: 600px">
      <template slot="paneL">
        <dir-tree ref="dirTree" :mHeight="530" :moduleUrl="moduleUrl" keyLabel="stdName" :moduleAuth="moduleAuth" :readonly="true" bssTitle="同义词" @node-click="nodeClickHandle"></dir-tree>
      </template>
      <template slot="paneR">
        <el-row type="flex" style="margin-bottom: 10px;">
          <el-input placeholder="名称"
                    @keyup.enter.native="getDataList()"
                    v-model="stdName"
                    clearable
                    @clear="getDataList()"
                    maxlength="30"
                    style="max-width: 300px;margin-right: 10px;"
                    class="input-with-select">
            <el-button slot="append"
                       icon="el-icon-search"
                       @click="getDataList()"></el-button>
          </el-input>
          <el-button style="margin-top: 5px;" type="primary" :disabled="btnDisabled" @click="synonymClick">添加</el-button>
        </el-row>
        <el-table :data="dataList" height="480px"
                  ref="table"
                  row-key="id"
                  border v-loading="dataListLoading"
                  @select="selectionChangeHandle"
                  @select-all="selectionChangeAllHandle"
                  size="mini">
          <el-table-column type="selection"
                           header-align="center"
                           align="center"
                           width="50"></el-table-column>
          <el-table-column label="标准（权威）名称"
                           prop="stdName"
                           min-width="150"
                           header-align="center"
                           align="left"
                           sortable></el-table-column>
          <el-table-column label="同义名称"
                           prop="synoName"
                           header-align="center"
                           sortable></el-table-column>
          <el-table-column label="创建人"
                           prop="createUser"
                           header-align="center"
                           align="left"
                           min-width="100"></el-table-column>
          <el-table-column label="创建时间"
                           prop="createTime"
                           header-align="center"
                           align="center"
                           min-width="120"></el-table-column>
          <el-table-column label="描述"
                           prop="remark"
                           header-align="center"
                           align="left"></el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pageSize"
          :total="totalCount"
          small
          layout="total, sizes, prev, pager, next, jumper"
          style="margin-top: 10px">
        </el-pagination>
      </template>
    </split-pane>
  </el-popover>
</template>

<script>
import dirTree from '../../../tree/dir-tree.vue'
export default {
  components: {
    dirTree
  },
  data () {
    return {
      visible: false,
      moduleUrl: 'std/syno/type/',
      moduleAuth: 'std:syno',
      pageIndex: 1,
      pageSize: 10,
      totalCount: 0,
      currentData: {id: '0'},
      stdName: '',
      dataList: [],
      dataListLoading: false,
      selectionMap: {},
      btnDisabled: true
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.$refs.dirTree.init()
    })
  },
  methods: {
    show () {
      this.pageIndex = 1
      this.pageSize = 10
      this.stdName = ''
      this.selectionMap = {}
      this.currentData = {id: '0'}
      this.getDataList()
    },
    nodeClickHandle (data) {
      this.currentData = data
      this.getDataList()
    },
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl(`/std/syno/page`),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'pid': this.currentData.id,
          'key': this.stdName,
          'isCat': this.currentData.isCat
        })
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalCount = data.page.totalCount
          this.$nextTick(() => {
            for (let item of this.dataList) {
              if (this.selectionMap.hasOwnProperty(item.id)) {
                this.$refs.table.toggleRowSelection(item)
              }
            }
          })
        } else {
          this.dataList = []
          this.totalCount = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 全选
    selectionChangeAllHandle (rows) {
      if (rows.length > 0) {
        for (let item of rows) {
          this.selectionMap[item.id] = item
        }
      } else {
        for (let item of this.dataList) {
          if (this.selectionMap.hasOwnProperty(item.id)) {
            delete this.selectionMap[item.id]
          }
        }
      }
      this.btnDisabled = Object.keys(this.selectionMap).length === 0
    },
    // 多选
    selectionChangeHandle (rows, row) {
      if (this.selectionMap.hasOwnProperty(row.id)) {
        delete this.selectionMap[row.id]
      } else {
        this.selectionMap[row.id] = row
      }
      this.btnDisabled = Object.keys(this.selectionMap).length === 0
    },
    synonymClick () {
      this.$emit('synonymClick', Object.values(this.selectionMap))
      this.visible = false
    }
  }
}
</script>

<style scoped>

</style>
